package com.mjh.zhujianju.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.livew.framework.base.dao.BaseMapper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.base.CommentMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.enums.SupervisionTypeEnum;
import com.mjh.zhujianju.model.base.CommentEO;
import com.mjh.zhujianju.model.supervise.SupervisionEstimateEO;
import com.mjh.zhujianju.service.base.CommentService;
import com.mjh.zhujianju.vo.base.CommentVO;
import com.mjh.zhujianju.vo.supervision.SupervisionEstimateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘洋
 * @Description: 评论
 * @date 2018/8/11 12:22
 */
@Service
public class CommentServiceImpl extends BaseServiceImpl<CommentEO> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private FileService fileService;

    public CommentServiceImpl(CommentMapper commentMapper) {
        super(commentMapper);
    }

    @Override
    public Integer queryCommentTotal(String type, Long businessId) {
        CommentEO commentEO = new CommentEO();
        commentEO.setType(type);
        commentEO.setBusinessId(businessId);
        return commentMapper.queryCommentTotal(commentEO);
    }

    @Override
    public List<CommentVO> queryCommentList(String type, Long businessId) {

        //1-查询所有评论
        CommentEO commentEO = new CommentEO();
        commentEO.setType(type);
        commentEO.setBusinessId(businessId);
        List<CommentVO> list= commentMapper.selectCommentList(commentEO);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }

        //2-查找子@评论人名称
        for (CommentVO comment1 : list){
            for (CommentVO comment2 : list){
                if (comment1.getToUserId()!=null&&comment1.getToUserId().equals(comment2.getUserId())){
                    comment1.setToUserName(comment2.getUserName());
                    continue;
                }
            }
        }

        //2-获取用户id
        List<Long> userIds = list.stream().map(commentVO -> commentVO.getUserId()).distinct().collect(Collectors.toList());

        //3-用户头像
        List<FileEO> fileEOList = fileService.getFileByIds(userIds,SysUser.class.getName(),null);
        if (CollectionUtils.isEmpty(fileEOList)){
            list.stream().forEach(commentVO -> {
                fileEOList.stream().forEach(fileEO -> {
                    if (commentVO.getUserId().equals(fileEO.getCaseId())){
                        commentVO.setUserFileId(fileEO.getId());
                    }
                });
            });
        }
        return list;
    }

    @Override
    public String addComment(CommentEO commentEO) {
        boolean b1 = commentEO.getType().equals(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        boolean c1 = StringUtils.isEmpty(commentEO.getUserId());
        boolean d1 = StringUtils.isEmpty(commentEO.getBusinessId());
        boolean isConet = StringUtils.isEmpty(commentEO.getContent());
        if (!b1 || c1 || d1 || isConet){
            return "parameter";
        }
        commentMapper.insertSelective(commentEO);
        return "success";
    }

    @Override
    public PagedResult<CommentVO> getPageList(CommentVO commentVO) {
        Integer pageNo = commentVO.getPageNo() == null ? 1 : commentVO.getPageNo();
        Integer pageSize = commentVO.getPageSize() == null ? 15 : commentVO.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        //1-查询所有评论
        CommentEO commentEO = new CommentEO();
        BeanUtils.copyProperties(commentVO,commentEO);
        commentEO.setType(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        List<CommentEO> list = commentMapper.selectPagination(commentEO);
        PagedResult<CommentEO> page = PagedBeanUtil.toPagedResult(list);
        PagedResult<CommentVO> result = new PagedResult<CommentVO>();
        BeanUtils.copyProperties(page, result);
        if (!XTListUtils.isEmpty(list)) {
            //2-获取用户id
            List<Long> userIds = list.stream().map(commentVO1 -> commentVO1.getUserId()).distinct().collect(Collectors.toList());

            List<CommentVO> voList = new ArrayList<CommentVO>();
            Map<Long, FileEO> files = fileService.getMapTopOne(userIds, SysUser.class.getName(), null);
            Map<Long,SysUser> sysUserMap = sysUserService.getMapTopOne(userIds);
            for (CommentEO commentEO1 : list) {
                CommentVO c = new CommentVO();
                BeanUtils.copyProperties(commentEO1, c);
                voList.add(c);
                FileEO file = files.get(c.getUserId());
                if (file != null) {
                    c.setUserFileId(file.getId());
                }

                SysUser sysUser = sysUserMap.get(c.getUserId());
                if(sysUser != null){
                    c.setUserName(sysUser.getNickname());
                }
            }
            result.setDataList(voList);
        }
        return result;
    }

    @Override
    public CommentEO selectCommentSingle(SupervisionEstimateVO supervisionEstimateVO) {
        CommentEO commentEO = new CommentEO();
        commentEO.setBusinessId(supervisionEstimateVO.getSupervisionId());
        commentEO.setType(SupervisionTypeEnum.visit.getCode());
        return commentMapper.selectCommentSingle(commentEO);

    }
}
